/*
 * $Id: Info.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.core.api;

import java.util.Map;

import net.sourceforge.p4eclipse.core.api.ClientUser;
import net.sourceforge.p4eclipse.core.api.IPerforceCommand;
import net.sourceforge.p4eclipse.core.api.IInfoResult;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 info' command.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Info extends ClientUser implements IPerforceCommand
{
	private Result result = new Result();	
	private IStatus status;
	
	/**
	  * Convenience method for creating instances of Dirs.
	  */
	public static final Info newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Info(status); 
	}

	/**
	  * Creates a new instance.
	  */
	private Info(MultiStatus status)
	{
		super(status);
		this.status = status;
	}	
	
	/**
	  * @see IClientUser#outputInfo(char, String)
	  */
	public void outputInfo(char level, String infoString)
	{
		if (level != '0')
			return;
		
		if (infoString.startsWith("User name:")) //$NON-NLS-1$
			result.userName = infoString.substring(11);
		else
		if (infoString.startsWith("Client name:")) //$NON-NLS-1$
			result.clientName = infoString.substring(13);
		else
		if (infoString.startsWith("Client host:")) //$NON-NLS-1$
			result.clientHost = infoString.substring(13);
		else
		if (infoString.startsWith("Client root:")) //$NON-NLS-1$
			result.clientRoot = infoString.substring(13);
		else
		if (infoString.startsWith("Client address:")) //$NON-NLS-1$
			result.clientAddress = infoString.substring(16);
		else
		if (infoString.startsWith("Server address:")) //$NON-NLS-1$
			result.serverAddress = infoString.substring(16);
		else
		if (infoString.startsWith("Server root:")) //$NON-NLS-1$
			result.serverRoot = infoString.substring(13);
		else
		if (infoString.startsWith("Server date:")) //$NON-NLS-1$
			result.serverDate = infoString.substring(13);
		else
		if (infoString.startsWith("Server version:")) //$NON-NLS-1$
			result.serverVersion = infoString.substring(16);
		else
		if (infoString.startsWith("Server license:")) //$NON-NLS-1$
			result.serverLicense = infoString.substring(16);
	}

	/**
	  * Returns the name of the p4 command of this instance.
	  */
	public String getCommand()
	{
		return "info"; //$NON-NLS-1$
	}
	
	/**
	  * Returns the result of the operation.
	  */
	public IInfoResult getResult()
	{
		return result;
	}
	
	/**
	  * Returns the IStatus object associated with the Fstat instance.
	  */
	public IStatus getStatus()
	{
		return status;
	}	
	
	/**
	  * Implementation of the IInfoResult. Stores information provided
	  * by the 'p4 info' command. This is just a dumb container.
	  * 
	  * @see IInfoResult
	  */
	private static class Result implements IInfoResult
	{
		private String userName;
		private String clientName;
		private String clientHost;
		private String clientRoot;
		private String clientAddress;
		private String serverAddress;
		private String serverRoot;
		private String serverDate;
		private String serverVersion;
		private String serverLicense;
		
		/** Creates a new Result object. */
		private Result() { }
	
		/** @see IInfoResult#getUserName() */
		public String getUserName() { return userName; }
	
		/** @see IInfoResult#getClientName() */
		public String getClientName() { return clientName; }
	
		/** @see IInfoResult#getClientHost() */
		public String getClientHost() { return clientHost; }
	
		/** @see IInfoResult#getClientRoot() */
		public String getClientRoot() { return clientRoot; }
			
		/** @see IInfoResult#getClientAddress() */
		public String getClientAddress() { return clientAddress; }
	
		/** @see IInfoResult#getServerAddress() */
		public String getServerAddress() { return serverAddress; }
	
		/** @see IInfoResult#getServerRoot() */
		public String getServerRoot() { return serverRoot; }
	
		/** @see IInfoResult#getServerDate() */
		public String getServerDate() { return serverDate; }
	
		/** @see IInfoResult#getServerVersion() */
		public String getServerVersion() { return serverVersion; }
	
		/** @see IInfoResult#getServerLicense() */
		public String getServerLicense() { return serverLicense; }
	}	
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return true;
	}	
}
